import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:ioc_pay_app/routes/HomeRoute.dart';
import 'package:ioc_pay_app/routes/LanguageRoute.dart';
import 'package:ioc_pay_app/routes/LoginRoute.dart';
import 'package:ioc_pay_app/routes/ThemeRoute.dart';
import 'package:ioc_pay_app/states/ProfileChangeNotifier.dart';
import 'package:provider/provider.dart';

import 'l10n/Localization_intl.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  void main() => runApp(new MyApp());
}

// 主页
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: <SingleChildCloneableWidget>[
        ChangeNotifierProvider.value(value: ThemeModel()),
        ChangeNotifierProvider.value(value: LocaleModel()),
        ChangeNotifierProvider.value(value: UserModel()),
      ],
      child: Consumer2<ThemeModel, LocaleModel>(
        builder: (BuildContext context, themeModel, localeModel, Widget child) {
          return MaterialApp(
            theme: ThemeData(
              primarySwatch: themeModel.theme,
            ),
            onGenerateTitle: (context) {
              return GmLocalizations.of(context).title;
            },
            home: HomeRoute(),
            locale: localeModel.getLocale(),
            supportedLocales: [
              const Locale('en', 'US'),
              const Locale('zh', 'Cn'),
            ],
            localizationsDelegates: [
              // 本地化代理类
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              //GmLocalizationsDelegate(),
            ],
            localeResolutionCallback:
                (Locale _locale, Iterable<Locale> supportedLocale) {
              if (localeModel.getLocale() != null) {
                return localeModel.getLocale();
              } else {
                //APP语言跟随系统语言，如果系统语言不是中文简体或美国英语，
                //则默认使用美国英语
                Locale locale;
                if (supportedLocale.contains(_locale)) {
                  locale = _locale;
                } else {
                  locale = Locale('en', 'US');
                }
                return locale;
              }
            },
            routes: <String, WidgetBuilder>{
              "login": (context) => LoginRoute(),
              "theme": (context) => ThemeRoute(),
              "language": (context) => LanguageRoute(),
            },
          );
        },
      ),
    );
  }
}
